package de.koller.million.grid.frame.enc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class GBar extends JPanel {
	
	private final IVolatileValue value;
	private Color barColor = Color.RED;
	private Color barFontColor = Color.BLACK;
	
	public GBar() {
		value = new IVolatileValue() {
			@Override public double getMaxValue() { return 1; }
			@Override public double getCurrValue() { return 0.5; }
		};		
	}

	@Override
	protected void paintComponent( Graphics g ) {
		super.paintComponent( g );
		Graphics2D g2 = (Graphics2D)g;
		
		if( value == null ) {
			g2.setColor( this.getBackground() );
			g2.fillRect( 0, 0, this.getWidth(), this.getHeight() );
		
			g2.setFont( this.getFont() );
			String t = "nicht besetzt";
			int tw = g2.getFontMetrics().stringWidth( t );
			int th = g2.getFontMetrics().getHeight();
			int tx = this.getWidth() / 2 - tw / 2;
			int ty = this.getHeight() / 2 - th / 2 + g2.getFont().getSize();
			return;
		}
		
		double v = value.getCurrValue() / value.getMaxValue();
		
		g2.setColor( this.getBackground() );
		g2.fillRect( 0, 0, this.getWidth(), this.getHeight() );
		
		int a = (int)( this.getWidth() * v );
		
		g2.setColor( barColor );
		g2.fillRect( 0, 0, a, this.getHeight() );
		
		g2.setFont( this.getFont() );
		String t = (int)value.getCurrValue() + " / " + (int)value.getMaxValue();
		int tw = g2.getFontMetrics().stringWidth( t );
		int th = g2.getFontMetrics().getHeight();
		int tx = this.getWidth() / 2 - tw / 2;
		int ty = this.getHeight() / 2 - th / 2 + g2.getFont().getSize();
		
		g2.clipRect( 0, 0, a, this.getHeight() );
		g2.setColor( barFontColor );
		g2.drawString( t, tx, ty );
		g2.setClip( null );
		
		g2.clipRect( a, 0, this.getWidth()-a, this.getHeight() );
		g2.setColor( getForeground() );
		g2.drawString( t, tx, ty );
		
		g2.setClip( null );
	}
	
	public GBar( IVolatileValue value ) {
		this.value = value;
	}

	public Color getBarColor() {
		return barColor;
	}

	public void setBarColor( Color barColor ) {
		this.barColor = barColor;
	}

	public Color getBarFontColor() {
		return barFontColor;
	}

	public void setBarFontColor( Color barFontColor ) {
		this.barFontColor = barFontColor;
	}

}
